#include "tanks.h"
#include "ui_tanks.h"
#include "RatingConvert.h"

Tanks::Tanks(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Tanks)
{
    ui->setupUi(this);
    getMitigation(0,0,0,0);
}

Tanks::~Tanks()
{
    delete ui;
}

void Tanks::compareUpgrades(){
    double d_armor = ui->armorBox_2->value();
    double d_def = ui->defBox_2->value();
    double d_absorb = ui->absorbBox_2->value();
    double d_shield = ui->shieldBox_2->value();
    double mitigation1 = getMitigation(d_armor, d_def, d_shield, d_absorb) - getMitigation(0, 0, 0, 0);
    ui->HPSlabel_2->setText(QString::number(mitigation1));
    d_armor = ui->armorBox_3->value();
    d_def = ui->defBox_3->value();
    d_absorb = ui->absorbBox_3->value();
    d_shield = ui->shieldBox_3->value();
    double mitigation2 = getMitigation(d_armor, d_def, d_shield, d_absorb) - getMitigation(0, 0, 0, 0);
    ui->HPSlabel_3->setText(QString::number(mitigation2));
    if(mitigation1 == mitigation2) ui->upg3Label->setText("No difference");
    if(mitigation1 > mitigation2) ui->upg3Label->setText("Use Item 1");
    if(mitigation1 < mitigation2) ui->upg3Label->setText("Use Item 2");
}
void Tanks::getWeights(){
    double armor = getMitigation(100, 0, 0, 0) - getMitigation(0, 0, 0, 0);
    double def = getMitigation(0, 1, 0, 0) - getMitigation(0, 0, 0, 0);
    double shield = getMitigation(0, 0, 1, 0) - getMitigation(0, 0, 0, 0);
    double absorb = getMitigation(0, 0, 0, 1) - getMitigation(0, 0, 0, 0);

    double refStat;
    if(ui->armorButton->isChecked())refStat = armor;
    if(ui->defenseButton->isChecked())refStat = def;
    if(ui->shieldButton->isChecked())refStat = shield;
    if(ui->absorbButton->isChecked())refStat = absorb;

    double armorWeight = armor/refStat;
    double defenseWeight = def/refStat;
    double shieldWeight = shield/refStat;
    double absorbWeight = absorb/refStat;
    ui->armorWeightLabel->setText(QString::number(armorWeight));
    ui->defenseWeightLabel->setText(QString::number(defenseWeight));
    ui->shieldWeightLabel->setText(QString::number(shieldWeight));
    ui->absorbWeightLabel->setText(QString::number(absorbWeight));
}

double Tanks::getMitigation(int d_armor, int d_def, int d_shield, int d_absorb){
    double attLevel = ui->attLevelBox->value();
    double level = ui->levelBox->value();
    double attAcc = 1+0.05*(attLevel-level), attCrit = 0.1, attSurge = 0.5;
    double genShield = 0, genAbsorb = 0;

    double base_DR;
    double base_armor_mult;
    double base_def_per;
    double base_shld_per;
    double base_abs_per;
    if(ui->shadow->isChecked()){
        base_DR = 2;
        base_armor_mult = 2.758;
        base_def_per = 16;
        base_shld_per = 35;
        base_abs_per = 24;
    }
    if(ui->vanguard->isChecked()){
        base_DR = 9;
        base_armor_mult = 1.775;
        base_def_per = 5;
        base_shld_per = 32;
        base_abs_per = 26;
    }
    if(ui->guardian->isChecked()){
        base_DR = 6;
        base_armor_mult = 1.615;
        base_def_per = 17;
        base_shld_per = 24;
        base_abs_per = 20;
    }
    double armor = ui->armorBox->value() + d_armor*base_armor_mult;
    double defense_rating = ui->defBox->value() + d_def;
    double shield_rating = ui->shieldBox->value() + d_shield;
    double absorb_rating = ui->absorbBox->value()+d_absorb;

    double armorDR= Armor(level, base_DR, armor);
    double defense = Defense(level, base_def_per, defense_rating);
    double shield = Shield(level, base_shld_per, shield_rating, genShield);
    double absorb = Absorb(level, base_abs_per, absorb_rating, genAbsorb);
    double mitigation = 100*(1 - ((min(attAcc - defense, 1.0)) * (1 - armorDR / 100)*(attCrit * (1 + attSurge) + min(shield, 1 - attCrit)*(1 - absorb) + max(1 - shield - attCrit, 0.0))));

    ui->defChanceBox->setText(QString::number(100*defense));
    ui->armor_DR_box->setText(QString::number(100*armorDR));
    ui->absorbPercentBox->setText(QString::number(100*absorb));
    ui->shieldChanceBox->setText(QString::number(100*shield));
    ui->mitLabel->setText(QString::number(mitigation));

    return mitigation;
}

void Tanks::on_shadow_clicked()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_vanguard_clicked()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_guardian_clicked()
{
    getMitigation(0,0,0,0);
}
void Tanks::on_armorBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_levelBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_attLevelBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_defBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_shieldBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_absorbBox_editingFinished()
{
    getMitigation(0,0,0,0);
}

void Tanks::on_upgradeButton_clicked()
{
    compareUpgrades();
}

void Tanks::on_calcWeightButton_clicked()
{
    getWeights();
}
